#include <iostream>
#include <thread>
#include <future>
#include<chrono>

int ADD(int num1, int num2)
{
    std::cout << "running the ADD func" << std::endl;
    return num1 + num2;
}

// std::async(policy, func_name, ...)   异步操作函数，参数：异步策略、函数名、函数参数列表
// std::launch::async为默认策略，std::launch::deferred为延迟策略
// async函数内部会创建新线程来处理函数！！！
void test1()
{
    std::cout << "--------1------" << std::endl;
    auto result = std::async(std::launch::async, ADD, 10, 25);
    std::this_thread::sleep_for(std::chrono::seconds(2));   // 在主线程处于休眠时，async线程会继续执行ADD
    std::cout << "--------2------" << std::endl;
    auto ret = result.get();
    std::cout << "the return value = " << ret << std::endl;
    std::cout << "--------3------" << std::endl;
}
void test2()
{
    std::cout << "--------1------" << std::endl;
    auto result = std::async(std::launch::deferred, ADD, 10, 25);
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "--------2------" << std::endl;
    auto ret = result.get();
    std::cout << "the return value = " << ret << std::endl;
    std::cout << "--------3------" << std::endl;
}
int main()
{
    test1();
    std::cout << std::endl;
    test2();
}